//
//  TripHeadCCell.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class TripHeadCCell: UICollectionViewCell {
    
    @IBOutlet weak var cardView: UIView!
    
    @IBOutlet weak var imageView: UIImageView!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var etitleLabel: UILabel!
    
    @IBOutlet weak var mengbanView: UIView!
    
    @IBOutlet weak var actionImageView: UIImageView!
    
//    let flightView : FlightView = FlightView()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        cardView.layer.cornerRadius = 8
        cardView.clipsToBounds = true
        actionImageView.addShadow(type: .btn)
        
        self.addShadow(type: .card)
        
//        self.contentView.addSubview(flightView)
//
//        self.flightView.snp.makeConstraints {
//
//            [weak self] make in
//            guard let `self` = self else { return }
//
//            make.edges.equalTo(self.imageView)
//        }
//
//        self.flightView.isHidden = true
    }
    
    var calendar:Calendar? {
        
        didSet {
            
            if calendar?.planType == PlanType.flight {
                
                imageView.image = UIImage(named: "flightBg")
//                self.flightView.isHidden = false
//                self.flightView.configData(model: calendar?.airplaneDetail)
                
                setupAirTitle()
                setupAirEtitle()
                
            } else {
                
                imageView.setImage(string: calendar?.pictureArray?.first, placeholder: UIImage(named:"placeholder_squ")!)
//                self.flightView.isHidden = true
                
                titleLabel.text = calendar?.title
                setupHotelEtitle()
            }
        }
    }
    
    
    private func setupAirTitle() {
        
        
        let att = NSMutableAttributedString()
        
        if let departureCity = calendar?.airplaneDetail?.departureCity,
            let destinationCity = calendar?.airplaneDetail?.arrivalCity {
            
            let att1 = NSMutableAttributedString(string: "\(departureCity) - \(destinationCity)")
            att1.yy_font = kFont12
            att1.yy_color = UIColor.hexStringToColor(hexString: "#222222")
            att.append(att1)
        }
        
        if let departureAirport = calendar?.airplaneDetail?.departureAirportObj,
            let departureIata = departureAirport.iata_code,
            let destinationAirport = calendar?.airplaneDetail?.arrivalAirportObj,
            let destinationIata = destinationAirport.iata_code {
            
            att.yy_appendString("\n")
            let att2 = NSMutableAttributedString(string: "\(departureIata) - \(destinationIata)")
            att2.yy_font = UIFont.systemFont(ofSize: 24)
            att2.yy_color = UIColor.hexStringToColor(hexString: "#222222")
            att.append(att2)
        }
        
        att.yy_lineSpacing = 5
        
        titleLabel.attributedText = att
    }
    
    private func setupAirEtitle() {
        
        let att = NSMutableAttributedString()
        
        if let departureDate = calendar?.airplaneDetail?.departureTime,
            let date = Date.date(departureDate, "yyyy-MM-dd HH:mm:ss") {
            
            var formatDate = date.string("MM-dd hh:mm aa",language: .chinese)
            
            if AppManager.share.language == .english {
                
                formatDate = date.string("dd MMM hh:mm aa",language: .english)
                
            }
            
            
            let att1 = NSMutableAttributedString(string: formatDate ?? "")
            att1.yy_font = kFont10
            att1.yy_color = UIColor.hexStringToColor(hexString: "#222222").withAlphaComponent(0.5)
            att.append(att1)
        }
        
        if let cname = calendar?.airplaneDetail?.departureAirportObj?.airport_cname {
            
            att.yy_appendString("\n")
            let att2 = NSMutableAttributedString(string: cname)
            att2.yy_font = kFont14
            att2.yy_color = UIColor.hexStringToColor(hexString: "#222222")
            att.append(att2)
            
        }
        att.yy_lineSpacing = 5
        
        etitleLabel.attributedText = att
    }
    
    private func setupHotelEtitle() {
        
        let att = NSMutableAttributedString()
        
        if let etitle = calendar?.etitle {
            
            let att1 = NSMutableAttributedString(string: etitle)
            att1.yy_font = kFont10
            att1.yy_color = UIColor.hexStringToColor(hexString: "#222222").withAlphaComponent(0.5)
            att.append(att1)
            att.yy_appendString("\n\n")
        }
        
        let att1 = NSMutableAttributedString()
        
        if let checkinDate = calendar?.checkinDate,
            let date = Date.date(checkinDate, "yyyy-MM-dd") {
            
            var formatDate = date.string("MM月dd日")
            
            if AppManager.share.language == .english {
                
                formatDate = date.string("dd MMM")
            }
            
            
            att1.yy_appendString(formatDate ?? "")
        }
        
        if let checkoutDate = calendar?.checkoutDate,
            let date = Date.date(checkoutDate, "yyyy-MM-dd") {
            
            var formatDate = date.string("MM月dd日")
            
            if AppManager.share.language == .english {
                
                formatDate = date.string("dd MMM")
            }

            att1.yy_appendString("-")
            
            att1.yy_appendString(formatDate ?? "")
        }
        
        if let night = calendar?.night {
            
            if AppManager.share.language == .english {
                
                if night > 1 {
                    
                    att1.yy_appendString(", \(night) nights")
                } else {
                    
                    att1.yy_appendString(", \(night) night")
                }
            } else {
                
                att1.yy_appendString(" (\(night)晚)")
            }
            
        }
        
        att1.yy_font = kFont14
        att1.yy_color = UIColor.hexStringToColor(hexString: "#222222")
        
        att.append(att1)
        att.yy_lineSpacing = 5
        
        etitleLabel.attributedText = att
    }
    
    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        
        super.apply(layoutAttributes)
        
        self.alpha = 1
        var alpha = layoutAttributes.alpha
        
        alpha = 1.1 - alpha
        
        self.mengbanView.backgroundColor = kMaskColor.withAlphaComponent(alpha * 1.7)
        
    }
}


